package com.ftg.shopping.sp_goodsinfo.controller;


import com.ftg.shopping.config.LayuiPage;
import com.ftg.shopping.sp_goodsinfo.entity.sp_goodsinfo;

import com.ftg.shopping.sp_goodsinfo.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.parameters.RequestBody;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Part;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Api(description ="商品管理")
@RestController
@RequestMapping("/api/sp_goodsinfo")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @ApiOperation("查看角色全部数据")
    @GetMapping("/list")
    public List<sp_goodsinfo> list(){ return goodsService.list(); }

    @ApiOperation("按主键查看一个角色")
    @PutMapping("/findone/{id}")
    public sp_goodsinfo list(@ApiParam("主键")@PathVariable int id){
        return goodsService.findone(id);
    }

    @ApiOperation("添加一个角色")
    @PostMapping("/insert")
    public  Map<String,Object> insertgoods(@RequestBody Part upload, sp_goodsinfo g)throws IOException{
        String path="C:\\Users\\Administrator\\Desktop\\nginx-1.19.4\\secound\\shangcheng\\res\\static\\img\\";

        String abc = UUID.randomUUID().toString();
        String type =  upload.getContentType();

        String subix = type.substring(type.lastIndexOf("/")+1,type.length());

        String fileName =path+"/"+ abc+"."+subix;

        upload.write(fileName);

        String dbFileName = abc+"."+subix;
        g.goodsphoto=dbFileName;
        goodsService.insertgoods(g);
        Map<String,Object> map=new HashMap<>();
        map.put("msg","success");
        map.put("code",0);
        map.put("src","");
        return map;
    }

    @ApiOperation("更改一个角色")
    @PostMapping("/update")
    public  int updategoods(@RequestBody sp_goodsinfo g){
        return  goodsService.updategoods(g);
    }

    @ApiOperation("删除一个角色")
    @DeleteMapping("/delete")
    public  int deletegoods( int id){
        return goodsService.deletegoods(id);
    }

    @ApiOperation("删除多个角色")
    @DeleteMapping("/delete1/{ids}")
    public  int deletegoodsIds(@PathVariable List<Integer> ids){
        return goodsService.deletegoodsIds(ids);
    }



    @ApiOperation("分页查询带模糊")
    @GetMapping("/page1")
    public LayuiPage<sp_goodsinfo> list1(@RequestParam int page, @RequestParam int limit, @RequestParam(required = false) String goodsname){
        return goodsService.testPage1(page,limit,goodsname);
    }

    @ApiOperation("获取所有商品")
    @GetMapping("/all")
    public LayuiPage<sp_goodsinfo> get(){ return goodsService.get(); }

    @ApiOperation("获取单一商品")
    @GetMapping("/one")
    public LayuiPage<sp_goodsinfo> get1(String goodsname){
        return goodsService.get1(goodsname);
    }







}
